package com.chen.authority.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.chen.authority.common.annotation.Log;
import com.chen.authority.common.enums.BusinessType;
import com.chen.authority.common.response.PageResponse;
import com.chen.authority.common.response.Response;
import com.chen.authority.common.response.SingleResponse;
import com.chen.authority.common.util.ExcelUtil;
import com.chen.authority.common.util.FileUtil;
import com.chen.authority.common.model.PageQo;
import com.chen.authority.model.dto.UserDto;
import com.chen.authority.model.qo.UserQo;
import com.chen.authority.model.vo.UserVo;
import com.chen.authority.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * 用户管理
 *
 * @author chengy
 * @date 2022/11/23
 */
@Api(tags = "用户管理 UserController")
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/user")
public class UserController {

    private final UserService userService;

    @PostMapping
    @ApiOperation("新增用户")
    @SaCheckPermission("system:user:add")
    @Log(module = "系统管理-用户管理", businessType = BusinessType.INSERT)
    public Response add(@Valid @RequestBody UserDto dto) {
        return userService.addOrUpdateUser(null, dto);
    }

    @DeleteMapping("/{ids}")
    @ApiOperation("删除用户")
    @SaCheckPermission("system:user:delete")
    @Log(module = "系统管理-用户管理", businessType = BusinessType.DELETE)
    public Response delete(@PathVariable Integer[] ids) {
        return userService.deleteUserByIds(ids);
    }

    @PutMapping("/{id}")
    @ApiOperation("更新用户")
    @SaCheckPermission("system:user:update")
    @Log(module = "系统管理-用户管理", businessType = BusinessType.UPDATE)
    public Response update(@PathVariable Integer id, @Valid @RequestBody UserDto dto) {
        return userService.addOrUpdateUser(id, dto);
    }

    @GetMapping("/{id}")
    @ApiOperation("查询用户详情")
    @SaCheckPermission("system:user:update")
    public SingleResponse<UserVo> get(@PathVariable Integer id) {
        return SingleResponse.success(userService.getUserInfoById(id));
    }

    @GetMapping("/list")
    @ApiOperation("查询用户列表")
    @SaCheckPermission("system:user:list")
    public PageResponse<UserVo> list(PageQo pageQo, UserQo userQo) {
        return PageResponse.success(userService.pageQueryUserList(pageQo, userQo));
    }

    @PostMapping("/import")
    @ApiOperation("导入用户列表")
    @SaCheckPermission("system:user:import")
    @Log(module = "系统管理-用户管理", businessType = BusinessType.IMPORT)
    public Response importUser(@ApiParam(value = "excel文件", required = true) MultipartFile file) {
        return FileUtil.isNotExcel(file) ? Response.fail("上传文件为空，或类型不为excel") : userService.importUserList(file);
    }

    @PostMapping("/export")
    @ApiOperation("导出用户列表")
    @SaCheckPermission("system:user:export")
    @Log(module = "系统管理-用户管理", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, UserQo qo) {
        ExcelUtil.exportExcel(response, "用户表", UserVo.class, userService.exportUserList(qo));
    }

}
